# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

# Scripting class with extensions
package dcExplorer::core::Fleet;
use strict;
use utf8;
use core::Group;
use base 'dcExplorer::core::Group';
# from_x, from_y, in_transit, script_active, script_ok, shares_count, tag, fleet_changes, owner - extended interface.
@dcExplorer::core::Fleet::props = 
	qw(id turns_till_arrival x y behaviour hidden name fly_range fly_speed large_ship_count sum_weight stealth_lvl free_carrier_capacity free_transport_capacity ext_name from_x from_y in_transit script_active script_ok shares_count tag fleet_changes owner);

# Scripting interface

sub fake
{
	my $self = shift;
	my $fake = shift;
	# TODO: Implement.
	die "Fleet::fake is not implemented yet.";
}

sub jump
{
	my $self = shift;
	my ($x, $y) = @_;
	return undef if $self->getProp('turns_till_arrival');
	return undef if $self->getProp('fleet_changes');
	return undef if scalar(grep $_->{'request_id'}, map {@{$_->getActions}} $self->getAllUnits);
	return undef if ($self->getProp('x') == $x && $self->getProp('y') == $y);
	my $dx = $x - $self->getProp('x');
	my $dy = $y - $self->getProp('y');
	my $d = sqrt($dx*$dx + $dy*$dy);
	return undef if ($self->getProp('fly_range') < $d);
	my $t = $d / $self->getProp('fly_speed');
	$t = int($t) + 1 if ($t > int($t));
	return $self->{Parent}->Move($self, $x, $y, $t);
}

sub show
{
	my $self = shift;
	my $show = shift;
	return undef unless $self->getProp('stealth_lvl') > 0;
	my ($hidden, $changes) = map {$self->getProp($_)} qw(hidden fleet_changes);
	$changes = defined($changes);
	if ($show)
	{
		if ($hidden)
		{
			if (!$changes)
			{
				$self->setProp('fleet_changes', 1);
				$self->{Parent}->HideAction($self, 'unhide_fleet');
			}
		}
		else
		{
			if ($changes)
			{	
				$self->setProp('fleet_changes', undef);
				$self->{Parent}->HideAction($self, 'cancelhide_fleet');
			}
		}
	}
	else
	{
		if (!$hidden)
		{
			if (!$changes)
			{
				$self->setProp('fleet_changes', 1);
				$self->{Parent}->HideAction($self, 'hide_fleet');
			}
		}
		else
		{
			if ($changes)
			{	
				$self->setProp('fleet_changes', undef);
				$self->{Parent}->HideAction($self, 'cancelunhide_fleet');
			}
		}
	}
	return 1;
}

sub SetBehaviour
{
	my $self = shift;
	my $newBehaviour = shift;
	$self->{'behaviour'} = $newBehaviour;
	return $self->{Parent}->SetFleetBehaviour($self);
}

sub Rename
{
	my $self = shift;
	my $newName = shift;
	my $newExtName = shift;
	$self->{'name'} = $newName;
	$self->{'ext_name'} = $newExtName;
	return $self->{Parent}->RenameFleet($self);
}

sub Share
{
	my $self = shift;
	my $playerNames = shift;
	my $mode = shift;
	# TODO: Implement.
	die "Fleet::Share is not implemented yet.";
}

# Scripting extensions

sub cancelJump
{
	my $self = shift;
	return undef if ($self->getProp('turns_till_arrival') == 0);
	return undef if ($self->getProp('in_transit') > 0);
	return $self->{Parent}->CancelMove($self);
}

sub getHideCost
{
	my $self = shift;
	my $hp = 0;
	$hp += $_->getProp('max_hitpoints') foreach $self->getAllUnits;
	my $result = ($hp ** 1.2) * $self->getProp('stealth_lvl') / 5;
	print $result."\n";
	return $result;
}

1;
